package com.bicon.botu.mall.goods.api;

import com.bicon.botu.mall.goods.commons.GoodsConstants;
import com.bicon.botu.mall.goods.service.IClassficationService;
import com.bicon.botu.mall.goods.vo.ClassficationVo;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 商品分类管理
 * Created by dsp on 2017/7/26 0026.
 */
@RestController
@RequestMapping("/V1.0.0/api/classficationApi")
public class ClassficationApiController extends BaseController {

    @Autowired
    private IClassficationService iClassficationService;    //商品分类Service

    /**
     * 根据父ID查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "findByClassifcationByParentId", method = RequestMethod.POST)
    public BaseResult findByClassifcationByParentId(String id) {
        //参数校验
        if (StringUtils.isNotBlank(id)) {
            //组装查询参数
            List<ClassficationVo> classficationList = iClassficationService.selectAllByParentId(id);
            //组装菜单树型结构
            if (classficationList != null && classficationList.size() > 0) {
                List<ClassficationVo> resultList = new ArrayList<>();
                for (ClassficationVo entity : classficationList) {
                    if (id.equals(entity.getParentId())) {//父级菜单开始添加
                        resultList.add(entity);
                        if (ifChilds(classficationList, entity.getId())) {//存在子集
                            getChildList(classficationList, entity);
                        }
                    }
                }
                return sendResult(GoodsConstants.RESPONSE_CODE_200, GoodsConstants.RESPONSE_MESSAGE_200,sortByDefaultComparer(resultList));
            } else {
                return sendResult(GoodsConstants.RESPONSE_CODE_602, GoodsConstants.RESPONSE_MESSAGE_602);
            }
        } else {
            return sendResult(GoodsConstants.RESPONSE_CODE_400, GoodsConstants.RESPONSE_MESSAGE_400);
        }
    }

    /**
     * 获取父id下的子集合
     *
     * @param list
     * @param classfication
     * @return
     */
    private static ClassficationVo getChildList(List<ClassficationVo> list, ClassficationVo classfication) {
        for (ClassficationVo entity : list) {
            if (entity.getParentId().equals(classfication.getId())) {//查询下级菜单
                classfication.getClassficationVoList().add(entity);
                if (ifChilds(list, entity.getId())) {
                    getChildList(list, entity);
                }
            }
        }
        return classfication;
    }

    /**
     * 判断是否存在子集
     *
     * @param list
     * @param pId
     * @return
     */
    private static boolean ifChilds(List<ClassficationVo> list, String pId) {
        boolean flag = false;
        for (ClassficationVo entity : list) {
            if (entity.getParentId().equals(pId)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * 集合排序
     * @param list
     */
    private List<ClassficationVo> sortByDefaultComparer(List<ClassficationVo> list)
    {
        if(list!=null && list.size()>0){
            Collections.sort(list, new Comparator<ClassficationVo>(){
                /*
                 * int compare(Student o1, Student o2) 返回一个基本类型的整型，
                 * 返回负数表示：o1 小于o2，
                 * 返回0 表示：o1和o2相等，
                 * 返回正数表示：o1大于o2。
                 */
                @Override
                public int compare(ClassficationVo o1, ClassficationVo o2) {
                    //按照学生的年龄进行升序排列
                    if(o1.getRank() > o2.getRank()){
                        return 1;
                    }
                    if(o1.getRank().equals(o2.getRank())){
                        return 0;
                    }
                    return -1;
                }
            });
            for(ClassficationVo classfication:list){
                if(classfication.getClassficationVoList()!=null
                        && classfication.getClassficationVoList().size()>0){
                    sortByDefaultComparer(classfication.getClassficationVoList());
                }
            }

        }
        return  list;
    }
}
